package com.ycode.bang.anbang.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ycode.bang.anbang.MyApp;
import com.ycode.bang.anbang.R;
import com.ycode.bang.anbang.adapter.PlotAdapter;
import com.ycode.bang.anbang.baseui.BaseActivity;
import com.ycode.bang.anbang.model.PlotName;
import com.ycode.bang.anbang.network.FinalHttpUtil;
import com.ycode.bang.anbang.utils.RequestMapDev;
import com.ycode.bang.anbang.utils.ToastUtil;

import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 选择小区界面
 */

public class SelectPlotActivity extends BaseActivity implements View.OnClickListener, TextWatcher {
    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.list)
    ListView lv;
    @InjectView(R.id.m_et_seach)
    EditText mEtSearch;

    PlotAdapter mAdapter;
    List<PlotName> list = new ArrayList<PlotName>();
    List<PlotName> copyList = new ArrayList<PlotName>();
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_plot);
        ButterKnife.inject(this);
        initView();
        initData();
    }

    public void initView() {
        getVillageList();
        title.setText("选择小区");
        mEtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(mEtSearch.getText().toString().toString())) {
                    List<PlotName> searchList = new ArrayList<PlotName>();
                    String f = mEtSearch.getText().toString().toString();
                    if (copyList.size() > 0) {
                        for (int i = 0; i < copyList.size(); i++) {
                            if (copyList.get(i).getVillageName().contains(f)) {
                                searchList.add(copyList.get(i));
                            }

                        }
                        list.clear();
                        list.addAll(searchList);
                        mAdapter.notifyDataSetChanged();
                    }
                } else {
                    list.clear();
                    list.addAll(copyList);
                    mAdapter.notifyDataSetChanged();
                }


            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void initData() {
//        for (int i = 0; i < 10; i++) {
//            if (i < 3) {
//                list.add(new PlotName("a小区" + i, "A", "2"));
//            } else if (i < 6) {
//                list.add(new PlotName("b小区" + i, "B", "2"));
//            } else {
//                list.add(new PlotName("c小区" + i, "C", "3"));
//            }
//        }
//        copyList.addAll(list);
//        mAdapter = new PlotAdapter(this, list);
//        lv.setAdapter(mAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(SelectPlotActivity.this, EditAddressActivity.class);
                intent.putExtra("plot", list.get(position).getVillageName());
                Bundle bundle = new Bundle();
                bundle.putString("plot",list.get(position).getVillageName());
                bundle.putString("plotid", list.get(position).getId());
                intent.putExtra("bundle", bundle);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    protected void getSuccessRequest(Object response, int flag) {

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    /**
     * 获取小区
     */
    private void getVillageList() {
        AjaxParams mAjaxParam = new AjaxParams();
        mAjaxParam.put("csrfToken", MyApp.instance.getToken());
        FinalHttpUtil.get(RequestMapDev.GetVillageList, mAjaxParam, new AjaxCallBack<String>() {
            @Override
            public void onSuccess(String o) {
                super.onSuccess(o);
                //放置数据
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    String status = jsonObject.getString("status");
                    String msg = jsonObject.getString("message");
                    String data = jsonObject.getString("data");
                    if (status.equals("0")) {
                        Log.e("这个接口是成功的", o);
                        Gson gson = new Gson();
                        list.clear();
                        list = gson.fromJson(data, new TypeToken<List<PlotName>>() {
                        }.getType());
                        mAdapter = new PlotAdapter(SelectPlotActivity.this, list);
                        copyList.addAll(list);
                        lv.setAdapter(mAdapter);
//                        LoginModel loginModel = gson.fromJson(data, LoginModel.class);
//                        MyApp.instance.setToken(loginModel.getCsrfToken());//放置token值
//                        MyApp.instance.setUserType(loginModel.getRealName());//放置登录角色
//                        ToastUtil.show(SelectPlotActivity.this, "登录成功");
//                        Intent intent = new Intent(SelectPlotActivity.this, HomepageActivity.class);
//                        startActivity(intent);
//                        finish();
                    } else {
                        ToastUtil.show(SelectPlotActivity.this, msg.toString());
                    }
                } catch (Exception e) {
                    ToastUtil.show(SelectPlotActivity.this, e.toString());
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                loginout_550(errorNo);
                ToastUtil.show(SelectPlotActivity.this, strMsg);
            }
        });
    }
}
